package com.sgb.kjwl.view.widget;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.lease.LeaseOrderEntity;
import com.sgb.kjwl.model.entity.lease.LeaseOrderInvoiceEntity;
import com.sgb.kjwl.view.ui.adapter.home.LeaseInvoiceAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;


/*
 * 租赁下单选择开具发票信息对话框
 */
public class LeaseOrderInvoiceDialog extends DialogFragment {

    private LeaseInvoiceAdapter adapter;

    private RecyclerView rv_invoice;
    private RadioGroup radioGroup;
    private RadioButton radioButton2;
    private RadioButton radioButton1;
    private List<LeaseOrderInvoiceEntity> list;
    private LeaseOrderInvoiceEntity invoiceEntity;
    private String flag;

    public static LeaseOrderInvoiceDialog newInstance(List<LeaseOrderInvoiceEntity> invoicelist, String flag) {
        LeaseOrderInvoiceDialog dialog = new LeaseOrderInvoiceDialog();
        Bundle bundle = new Bundle();
        bundle.putString("flag", flag);
        bundle.putSerializable("invoicelist", (Serializable) invoicelist);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_lease_order_invoice, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        radioButton1 = view.findViewById(R.id.rb_1);
        radioButton2 = view.findViewById(R.id.rb_2);
        radioGroup = view.findViewById(R.id.rg_invoice);
        rv_invoice = view.findViewById(R.id.rv_invoice);

        view.findViewById(R.id.tv_tip).setOnClickListener(v -> MToast.showToast(getActivity(), "请联系管理员新增开票信息"));
        view.findViewById(R.id.back_view).setOnClickListener(v -> dismiss());
        view.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (itemCallBack != null) {
                    if (radioGroup.getCheckedRadioButtonId() == radioButton2.getId()) {
                        itemCallBack.onSelect(null);
                    } else {
                        itemCallBack.onSelect(invoiceEntity);
                    }

                }
            }
        });

        initArguments();
        initAdapter();
    }

    private void initArguments() {
        flag = getArguments().getString("flag");
        list = (List<LeaseOrderInvoiceEntity>) getArguments().getSerializable("invoicelist");
        if ("是".equals(flag)) {
            radioGroup.check(radioButton1.getId());
        } else {
            radioGroup.check(radioButton2.getId());
        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.Bottom_Dialog_Animation;
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM);
        DisplayMetrics dpMetrics = new DisplayMetrics();
        window.getWindowManager().getDefaultDisplay().getMetrics(dpMetrics);
        WindowManager.LayoutParams p = window.getAttributes();

        p.dimAmount = 0.5f;
        p.width = dpMetrics.widthPixels;
        p.height = dpMetrics.heightPixels * 3 / 4;
        window.setAttributes(p);
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new LeaseInvoiceAdapter(getActivity(), list);
        }
        adapter.setOnOnItemClickListener(new LeaseInvoiceAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(LeaseOrderInvoiceEntity entity) {
                invoiceEntity = entity;
            }
        });
        rv_invoice.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_invoice.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getActivity().getResources().getDimension(R.dimen.dp6), getActivity().getResources().getColor(R.color.white)));
        rv_invoice.setAdapter(adapter);
    }

    public void showAllowingLoss(FragmentManager manager, String tag) {
        try {
            Class cls = DialogFragment.class;
            Field mDismissed = cls.getDeclaredField("mDismissed");
            mDismissed.setAccessible(true);
            mDismissed.set(this, false);
            Field mShownByMe = cls.getDeclaredField("mShownByMe");
            mShownByMe.setAccessible(true);
            mShownByMe.set(this, true);
        } catch (Exception e) {
            show(manager, tag);
            return;
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void dismiss() {
        if (getFragmentManager() == null) return;
        super.dismissAllowingStateLoss();
    }

    private ItemCallBack itemCallBack;

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemCallBack {
        void onSelect(LeaseOrderInvoiceEntity invoiceEntity);
    }

}
